/**
 * @Description: 全屏路由 权限
 * @Author bianpengfei
 * @create 2022/11/24 20:33
 */
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { cache } from 'sf-utils2'
import { $initDict, $initParam } from '@/utils/dict.ts'
import { MODULE_TYPE } from '@/router/routes.ts'

// types

NProgress.configure({ showSpinner: false })
/**
 * 路由钩子鉴权
 */
export default function routerMiddlewareAuth() {
  // const tempRoutes = this.getRoutes()
  // console.log('tempRoutes', tempRoutes)

  /**
   * 路由鉴权
   */
  this.beforeEach(async (to, from, next) => {
    NProgress.start()
    if (to.__skip === true) return next()
    if (to.meta.module == MODULE_TYPE.SERVICE_PROVIDER) {
      // alert()
      const pendings = []
      // 判断是否已经成功加载字典
      if (!cache?.getItem('DICT')) pendings.push($initDict())
      if (!cache?.getItem('PARAM')) pendings.push($initParam())
      await Promise.all(pendings)

      to.__skip = true
      return next()
    }
    next()
  })

  this.afterEach(() => {
    // 在非乾坤中 关闭加载进度条
    NProgress.done()
  })
}
